package com.jiepos.mpos.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.core.dao.BaseDao;

import oracle.jdbc.driver.OracleTypes;

@Repository
public class TransAmountCalDao extends BaseDao<App_Custom_InfEntity> {
	
	@SuppressWarnings({"unchecked","rawtypes"})
	public Map<String,String> getTransAmountInfo(final String i_amount_type,final String i_custom_login_id,final String i_trace_no){
		Map<String,String> map = (Map)  jdbcTemplate.execute(new CallableStatementCreator() {
			public CallableStatement createCallableStatement(Connection con) throws SQLException {
				String storedProc = "{CALL PACKAGE_MEMBER.P_CUSTOM_AMOUNT (?,?,?,?,?,?,?,?)}";// 调用的sql
				CallableStatement cs = con.prepareCall(storedProc);
				cs.setString(1, i_amount_type);// 金额计算类型，1-推荐注册，2-充值，3-消费
				cs.setString(2, i_custom_login_id);// 客户登陆账号
				cs.setString(3, i_trace_no);// 交易流水号
				cs.registerOutParameter(4, OracleTypes.VARCHAR);// 客户手机号
				cs.registerOutParameter(5, OracleTypes.VARCHAR);// 直接推荐人clientid
				cs.registerOutParameter(6, OracleTypes.NUMBER);// 直接推荐佣金
				cs.registerOutParameter(7, OracleTypes.VARCHAR);// 间接推荐人clientid
				cs.registerOutParameter(8, OracleTypes.NUMBER);// 间接推荐佣金
				return cs;
			}
		}, new CallableStatementCallback() {
			public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
				cs.execute();
				Map<String,String>	map = new HashMap<String,String>();
				map.put("o_custom_phone", cs.getString(4));
				map.put("o_direct_custom_uuid", cs.getString(5));
				map.put("o_direct_amount", String.valueOf(cs.getObject(6)));
				map.put("o_indirect_custom_uuid", cs.getString(7));
				map.put("o_indirect_amount", String.valueOf(cs.getObject(8)));
				log.debug("计算客户佣金[{}]查询对应的信息:[{},{},{}]",i_amount_type,i_custom_login_id,i_trace_no,map);
				return map;// 获取输出参数的值
			}
		});
		return map;
	}

}
